package com.vue.util;

/**
 * @author: wrj
 * @since: 2023/6/9 12:09
 * @description:
 */

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.ListObjectsRequest;
import com.aliyun.oss.model.ObjectListing;
import com.aliyun.oss.model.OSSObjectSummary;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

public class ListObjectsSample {

    public static void main(String[] args) {
        String endpoint = "oss-cn-beijing.aliyuncs.com";
        String accessKeyId = "LTAI5tD5EN8RevhJUWgFYhib";
        String accessKeySecret = "pu6IAbouw36HkAkwwsmhemG1ITzeCP";
        String bucketName = "shiye-test";
        String prefix = "https://shiye-test.oss-cn-beijing.aliyuncs.com/sddc/2023-06/2023-06-09/";  // 目录路径，以“/”结尾

        // 创建OSSClient实例。
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        // 构造ListObjectsRequest请求
        ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucketName);
        // 设置prefix参数来获取fun目录下的所有文件。
        listObjectsRequest.setPrefix(prefix);
        // 列出文件。
        ObjectListing listing = ossClient.listObjects(listObjectsRequest);
        List<OSSObjectSummary> summaries = listing.getObjectSummaries();
        if (summaries.size()>0){
            // 遍历所有文件。
            List<String> list = new ArrayList<>();
            for (int i = 0; i < summaries.size(); i++) {
                if (i == 0) {
                    continue;
                }
                String FILE_URL = "https://" + bucketName + "." + endpoint + "/" + summaries.get(i).getKey();
                list.add(FILE_URL);
            }
            // 关闭OSSClient。
            ossClient.shutdown();

            for (String s:list) {
                System.out.println("返回："+s);
            }
        }

    }
}
